package org.seefly.iprint.service.impl;

import org.seefly.iprint.entity.Admin;
import org.seefly.iprint.entity.AdminExample;
import org.seefly.iprint.mapper.AdminMapper;
import org.seefly.iprint.service.AdminService;
import org.seefly.iprint.utils.MD5Utils;
import org.seefly.iprint.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper dao;
    
    /**
     * 登陆校验
     */
    @Override
    public Admin adminLogin(LoginUserVo vo) {
        //首先通过用户名进行查询
        AdminExample ae = new AdminExample();
        ae.createCriteria().andAccountEqualTo(vo.getName());
        //再数据库中通过用户名查询
        List<Admin> list = dao.selectByExample(ae);
        //判断该用户名是否存在
        if(list == null || list.size() < 1) {
            return null;
        }
        Admin admin = list.get(0);
        //获取用户盐值
        String salt = admin.getSalt();
        //将用户输入的密码与数据库中盐值进行加密运算
        String pwd = MD5Utils.md5_salt(vo.getPwd(), salt);
        
        //将运算结果与数据库中的值进行比较
        if(admin.getPassword().equals(pwd)) {
            //更新登陆ip
            admin.setLoginIp(vo.getLoginIP());
            //更新登陆次数
            admin.increaseLoginCount();
            //更新登陆时间
            admin.updateLoginTime();
            //写入数据库
            dao.updateByPrimaryKeySelective(admin);
            return admin;
        }
        return null;
    }

     
}
